﻿ <?php
/*****************************************
Wer Wird Millionär
TIT10NS Webengeneering Projekt

TAdminView - Backend-View-Klasse

Autoren: Markus Schwarz
Copyright: 2012 DHBW Mannheim
*****************************************/
class TAdminView extends TView{


/******************** Prüffunktionen ********************/
// Überprüft, ob User Admin
private static function isAdmin() {
	
	if (isset($_SESSION['user']))
	{
		if (($_SESSION['user']->getRole()=='admin'))
		{
			return true;
		}
		else
		{	
			$notification = new TAdminNotification("error", "generalMissingPermission");
			self::showNotificationtext($notification);
			return false;
		}
	}
	else
	{
		$notification = new TAdminNotification("error", "generalNotLoggedIn");
		self::showNotificationtext($notification);
		return false;
	}
}

// Wenn Werte vorhanden sind, werden diese in die Felder eingetragen
// Ausnahme: Transaktion war erflogreich --> Felder bleiben leer!
private static function showValues($var, $notification){
	if ($var != "" AND $notification->getNotificationClass() != "success"){
		return "value = '$var'";
	}
}

// Überprüft bei den Select-Feldern, ob schon welche vorher selektiert wurden
private static function isSelected($var, $value, $question, $notification) {
	
	if ($notification->getNotificationClass() != "success") {
		switch ($var) {
			case "categories":
				if ($question->getCategories() != "") {
					if (in_array($value, $question->getCategories(), true)) {
						return "selected";
					}
				}
				break;
				
			case "difficulty":
				if ($question->getDifficulty() != "") {
					if ($question->getDifficulty()== $value) {
						return "selected";
					}
				}
				break;
		}
	}
}

private static function showNotificationtext($notification) {
	echo 	"<div id='notificationtext' class='".$notification->getNotificationClass()."'>"
			.parent::getAdminText("notification", $notification->getNotificationText())."</div> \n <br />";
}

/******************** Funktionen zur Darstellung der Formulare ********************/

// Darstellung der Navigationsseite
public static function showNavMenu() {
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		echo "
		<a class='pageText' href='/web-eng-wwm/newquestion.php'>".parent::getAdminText("navigation", "newQuestion")."</a><br />
		<a class='pageText' href='/web-eng-wwm/changequestion.php'>".parent::getAdminText("navigation", "changeQuestion")."</a><br />
		<a class='pageText' href='/web-eng-wwm/newcategory.php'>".parent::getAdminText("navigation", "newCategory")."</a><br />
		<a class='pageText' href='/web-eng-wwm/changecategory.php'>".parent::getAdminText("navigation", "changeCategory")."</a><br />
		<a class='pageText' href='/web-eng-wwm/resultnames.php'>".parent::getAdminText("navigation", "changeResults")."</a><br />
		<a class='pageText' href='/web-eng-wwm/jokerpossibilities.php'>".parent::getAdminText("navigation", "changeJoker")."</a><br />
		<a class='pageText' href='/web-eng-wwm/manageadmins.php'>".parent::getAdminText("navigation", "manageAdmins")."</a>";
	}
}

// Gewinnstufen anpassen
public static function getResultForm($notification, $levels = ""){
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}

		
		echo "<form class='results' action='resultnames.php' method = 'POST'> \n";
					
		echo "<table class='resultchange'>
			<tr class='resultchange'>
				<th class='resultchange' colspan = '2'>".parent::getAdminText("resultForm", "tableTitle")."</th>
			</tr>
			<tr class='resultchange'>
				<td class='resultchange'>".parent::getAdminText("resultForm", "titleIDColumn")."</td>
				<td class='resultchange'>".parent::getAdminText("resultForm", "titleNameColumn")."</td>
			</tr> \n";
		if ($notification->getNotificationClass() != "error") {
			$results = TModel::getAllResults();
			
			foreach ($results as $id => $name) {
				echo 
				'<tr class="resultchange">
					<td class="resultchange">'.$id.'</td>
					<td class="resultchange"><input class="resultchange" type = "text" name = "level'.$id.'" value = "'.$name.'"></td>
				</tr>'."\n"; 
			}			
		}
		else {
			$i = 0;
			foreach ($levels as $level) {
				echo 
				'<tr class="resultchange">
					<td class="resultchange">'.$i.'</td>
					<td class="resultchange"><input class="resultchange" type = "text" name = "level'.$i.'" value = "'.$level.'"></td>
				</tr>'."\n"; 
				
				$i = $i + 1;
			}
		}
		
		echo "\n </table><br /> \n";
		echo "<input class='submit' type='submit' name ='submitButton' value='".parent::getAdminText("resultForm", "submitButton")."'> \n";
		echo "</form>";
	}
}

// Gewinnstufen anpassen
public static function getJokerForm($notification, $jokers = ""){
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}

		echo "<div id='results'>";
		echo "<form class='results' action='jokerpossibilities.php' method = 'POST'> \n";
					
		echo "<table class='resultchange' text-align = 'center' border = '1' align = 'center'>
			<tr class='resultchange'>
				<th class='resultchange' colspan = '2' width = '200'>".parent::getAdminText("jokerForm", "tableTitle")."</th>
			</tr>
			<tr class='resultchange'>
				<td class='resultchange'>".parent::getAdminText("jokerForm", "jokerNameColumn")."</td>
				<td class='resultchange'>".parent::getAdminText("jokerForm", "probabilityColumn")."</td>
			</tr> \n";
			
		if ($notification->getNotificationClass() != "error") {
			$jokers = TModel::getJokerPossibilities();
		}
		
		self::fillJokerTableRow("TelSureLow", $jokers->getTelSureLow());
		self::fillJokerTableRow("TelSureLowMed", $jokers->getTelSureLowMed());
		self::fillJokerTableRow("TelSureMed", $jokers->getTelSureMed());
		self::fillJokerTableRow("TelSureMedHigh", $jokers->getTelSureMedHigh());
		self::fillJokerTableRow("TelSureHigh", $jokers->getTelSureHigh());
		
		self::fillJokerTableRow("TelNoIdeaLow", $jokers->getTelNoIdeaLow());
		self::fillJokerTableRow("TelNoIdeaLowMed", $jokers->getTelNoIdeaLowMed());
		self::fillJokerTableRow("TelNoIdeaMed", $jokers->getTelNoIdeaMed());
		self::fillJokerTableRow("TelNoIdeaMedHigh", $jokers->getTelNoIdeaMedHigh());
		self::fillJokerTableRow("TelNoIdeaHigh", $jokers->getTelNoIdeaHigh());
		
		self::fillJokerTableRow("AudLow",  $jokers->getAudLow());
		self::fillJokerTableRow("AudLowMed", $jokers->getAudLowMed());
		self::fillJokerTableRow("AudMed", $jokers->getAudMed());
		self::fillJokerTableRow("AudMedHigh", $jokers->getAudMedHigh());
		self::fillJokerTableRow("AudHigh", $jokers->getAudHigh());
		
		self::fillJokerTableRow("RiskLow",  $jokers->getRiskLow());
		self::fillJokerTableRow("RiskLowMed", $jokers->getRiskLowMed());
		self::fillJokerTableRow("RiskMed", $jokers->getRiskMed());
		self::fillJokerTableRow("RiskMedHigh", $jokers->getRiskMedHigh());
		self::fillJokerTableRow("RiskHigh",$jokers->getRiskHigh());		
		
		echo "\n </table><br /> \n";
		echo "<input class='submit' type='submit' name ='submitButton' value='".parent::getAdminText("jokerForm", "submitButton")."'> \n";
		echo "</form></div>";
	}
}

// genutzt, um eine einzelne Zeile der Jokertabelle darzustellen, wird dadurch mehrfach von getJokerForm abgerufen
private static function fillJokerTableRow($name, $jokerValue) {	
	echo 
	'<tr class="resultchange">
		<td class="resultchange">'.parent::getAdminText("jokerForm", $name).'</td>
		<td class="resultchange"><input class="resultchange" type = "text" name = "'.$name.'" value = "'.$jokerValue.'"></td>
	</tr>'."\n"; 
}

// Neue Frage anlegen
public static function getQuestionForm($question, $notification) {
	$checkAdmin = self::isAdmin();
	
	// Abfrage der Kategorien
	$result = TModel::getCategories();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}
		
		if ($question->getQuestionID() == "new") {
			echo "\n <form action='newquestion.php' method = 'POST'> \n";
		}
		else {
			echo "\n <form action='changequestion.php' method = 'POST'> \n";
		}
		
		echo "<div id='newquestion'> \n";
		echo "<p class='pageText'>".parent::getAdminText("questionForm", "categoryCaption")." \n";				
		echo "<select class='category' name='categories[]' size = '5' multiple> \n";
		
		foreach ($result as $id => $name) {
			echo "	<option ".self::isSelected('categories', $name, $question, $notification).">".$name."</option> \n";
		}
		
		echo "</select> \n";
		echo "</p> \n";			
		echo 	"<p class='pageText'>".parent::getAdminText("questionForm", "difficultyCaption")."
					<select class='difficulty' name='difficulty'>
						<option ".self::isSelected('difficulty', parent::getAdminText("questionForm", "diffLow"), $question, $notification).">".parent::getAdminText("questionForm", "diffLow")."</option>
						<option ".self::isSelected('difficulty', parent::getAdminText("questionForm", "diffLowMed"), $question, $notification).">".parent::getAdminText("questionForm", "diffLowMed")."</option>
						<option ".self::isSelected('difficulty', parent::getAdminText("questionForm", "diffMed"), $question, $notification).">".parent::getAdminText("questionForm", "diffMed")."</option>
						<option ".self::isSelected('difficulty', parent::getAdminText("questionForm", "diffMedHigh"), $question, $notification).">".parent::getAdminText("questionForm", "diffMedHigh")."</option>
						<option ".self::isSelected('difficulty', parent::getAdminText("questionForm", "diffHigh"), $question, $notification).">".parent::getAdminText("questionForm", "diffHigh")."</option>
					</select>
				</p>";
				
		if ($question->getQuestionID() != "new") {
			echo "<p class='pageText'>I".parent::getAdminText("questionForm", "IDcaption")." <input class='questionID' name='questionID' type='text' ".self::showValues($question->getQuestionID(), $notification)." size = '5' readonly></p>";
		}
		
		echo   "<p class='pageText'>".parent::getAdminText("questionForm", "questionCaption")." <input class='questiontext' name='questiontext' type='text' ".self::showValues($question->getQuestionText(), $notification)."></p>
				<p class='pageText'>".parent::getAdminText("questionForm", "rightAnswerCaption")."  <input class='right' name='right' type='text' ".self::showValues($question->getRight(), $notification)."></p>
				<p class='pageText'>".parent::getAdminText("questionForm", "wrongAnswer1Caption")." <input class='wrong' name='wrong1' type='text' ".self::showValues($question->getWrong1(), $notification)."></p>
				<p class='pageText'>".parent::getAdminText("questionForm", "wrongAnswer2Caption")." <input class='wrong' name='wrong2' type='text' ".self::showValues($question->getWrong2(), $notification)."></p>
				<p class='pageText'>".parent::getAdminText("questionForm", "wrongAnswer3Caption")." <input class='wrong' name='wrong3' type='text' ".self::showValues($question->getWrong3(), $notification)."></p>
				<input class='submit' type='submit' name ='questionButton' value='".parent::getAdminText("questionForm", "submitButton")."'>
				<input class='submit' type='reset' name='resetButton' value='".parent::getAdminText("questionForm", "resetButton")."'>
				</div> </form>";
	}
}
// Bestehende Frage ändern --> Vorauswahl der Frage, danach wird getQuestionForm genutzt!
public static function getChangeQuestionForm($notification, $categoryID="") {
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}
	
		echo "<form action='changequestion.php' method = 'POST'> \n";
		
		echo "<p><select class='change' name='category' size = '1'> \n";
		echo "<option value = '0' >".parent::getAdminText("changeQuestionForm", "allCategoriesSelect")."</option> \n";
		$result = TModel::getCategories();
		
		foreach ($result as $id => $name) {
			if (TModel::countQuestionsInCategory($id) > 0) {
				if ($categoryID == $id) {
					echo "<option value = '".$id."' selected>".$name."</option> \n";				
				}
				else {
					echo "<option value = '".$id."' >".$name."</option> \n";
				}
			}
		}
		
		echo "</select><input class='submit' type='submit' name ='setFilter' value='".parent::getAdminText("changeQuestionForm", "filterButton")."'><br /> <br />\n";
		
		echo "<p><select class='change' name='question' size = '25'> \n";
		
		if ($categoryID != "") {
			$result = TModel::getQuestionsByCategory($categoryID);
		}
		else {
			$result = TModel::getQuestionsByCategory();
		}
		
		foreach ($result as $id => $question) {
			echo "<option value = '".$id."' >".$question."</option> \n"; 
		}
		
		echo "</select><br /> <br />\n";
			
		echo "<input class='submit' type='submit' name ='submitChoice' value='".parent::getAdminText("changeQuestionForm", "submitButton")."'>";
		echo "<input class='submit' type='reset' name='resetButton' value='".parent::getAdminText("changeQuestionForm", "resetButton")."'>";
		echo "</form>";
	
	}
}

// Bestehende Kategorie ändern --> Vorauswahl der Kategorie, danach wird getCategoryForm genutzt!
public static function getChangeCategoryForm($notification) {
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}
	
		echo "<form action='changecategory.php' method = 'POST'> \n";
		
		echo "<p><select class='change' name='category' size = '25'> \n";
		
		$result = TModel::getCategories();
		
		foreach ($result as $id => $name) {
			echo "<option value = '".$id."' >".$name."</option> \n"; 
		}
		
		echo "</select><br /> <br />\n";
			
		echo "<input class='submit' type='submit' name ='submitChoice' value='".parent::getAdminText("changeCategoryForm", "submitButton")."'>";
		echo "<input class='submit' type='reset' name='resetButton' value='".parent::getAdminText("changeCategoryForm", "resetButton")."'>";
		echo "</form>";
	
	}
}

// Neue Kategorie anlegen
public static function getNewCategoryForm($notification) {
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}
		
		echo '<div id="category">';
		echo	"<form action='newcategory.php' method = 'POST'>
					<p class='pageText'>Kategoriename: <input name='newCategory' type='text'></p>
					<input class='submit' type='submit' name ='categoryButton' value='".parent::getAdminText("newCategoryForm", "submitButton")."'>
					<input class='submit' type='reset' name='resetButton' value='".parent::getAdminText("newCategoryForm", "resetButton")."'>
				</form></div>";
	}
}

// Formular um Kategorienamen und zugehörige Fragen zu ändern
public static function getCategoryForm($category, $notification) {
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}
		
		echo '<div id="category">';
		if ($category->getCategoryID() == "new") {
			echo	"<form action='newcategory.php' method = 'POST'>
						<p class='pageText'>Kategoriename: <input name='catName' type='text' value='".$category->getCategoryName()."'></p>";
			echo "<input class='submit' type='submit' name ='catButton' value='Submit'>";
			echo "<input class='submit' type='reset' name='resetButton' value='Reset'>";
			echo "</form></div>";
		}
		else {
			echo	"<form action='changecategory.php' method = 'POST'>
						<p class='pageText'>".parent::getAdminText("categoryForm", "IDcaption")." <input name='catID' type='text' value='".$category->getCategoryID()."' size = '5' readonly></p>
						<p class='pageText'>".parent::getAdminText("categoryForm", "categoryCaption")." <input name='catName' type='text' value='".$category->getCategoryName()."'></p>";
			echo "<p><select class='change' name='questions[]' size = '25' multiple> \n";
		
			$categoryQuestions = TModel::getQuestionsByCategory($category->getCategoryID());
			$allQuestions = TModel::getQuestionsByCategory();

			foreach ($allQuestions as $id => $question) {
				if (in_array($question, $categoryQuestions, true)){
					echo "<option value = '".$id."' selected>".$question."</option> \n"; 
				}
				else {
					echo "<option value = '".$id."' >".$question."</option> \n"; 
				}
			}

			echo "</select><br /> <br />\n";
				
			echo "<input class='submit' type='submit' name ='catButton' value='".parent::getAdminText("categoryForm", "submitButton")."'>";
			echo "<input class='submit' type='reset' name='resetButton' value='".parent::getAdminText("categoryForm", "resetButton")."'>";
			echo "</form></div>";
		}
	}
}

public static function getManageAdminForm($notification, $users = "", $countUsers = "") {
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}
	
		echo "<form action='manageadmins.php' method = 'POST'> \n";
		echo "<p class='pageText'>".parent::getAdminText("manageAdminForm", "userNameCaption")." <input name='searchWord' type='text'>
			 <input class='submit' type='submit' name ='searchUsers' value='".parent::getAdminText("manageAdminForm", "searchButton")."'></p><br />";
		echo "<input class='submit' type='submit' name ='findAdmins' value='".parent::getAdminText("manageAdminForm", "searchAdminButton")."'>";
		
		if ($users != "") {
			echo "<p><select class='change' name='users' size = '".$countUsers."'> \n";
			
			foreach ($users as $id => $username) {
				echo "<option value = '".$id."' >".$username."</option> \n"; 
			}
			
			echo "</select><br /> <br />\n";
				
			echo "<input class='submit' type='submit' name ='submitChoice' value='".parent::getAdminText("manageAdminForm", "submitButton")."'>";
			echo "<input class='submit' type='reset' name='resetButton' value='".parent::getAdminText("manageAdminForm", "resetButton")."'>";
		}
		
		echo "</form>";
	
	}
}

public static function getManageUserRightsForm($notification, $user) {
	$checkAdmin = self::isAdmin();
	
	if ($checkAdmin==true) {
		if ($notification->getNotificationText() != "") {
			self::showNotificationtext($notification);
		}
	
		echo "<form action='manageadmins.php' method = 'POST'> \n";
		echo "<p class='pageText'>".parent::getAdminText("manageUserRightsForm", "IDcaption")." <input name='ID' value = '".$user->getUserid()."' type='text' readonly></p>";
		echo "<p class='pageText'>".parent::getAdminText("manageUserRightsForm", "userNameCaption")." <input name='username' value = '".$user->getNickname()."' type='text' readonly></p>";
		
		if ($user->getRole() == 'admin') {
			echo "<span class='pageText'>".parent::getAdminText("manageUserRightsForm", "roleAdmin")."</span>	<Input type ='radio' id='radio-1-1' name ='rights' value= 'admin' class='radio' checked><label for='radio-1-1'></label>
			&nbsp;<span class='black'>|</span>&nbsp;";
			echo "<span class='pageText'>".parent::getAdminText("manageUserRightsForm", "roleUser")."</span>	<Input type ='radio' id='radio-1-2' name ='rights' value= 'user' class='radio'><label for='radio-1-2'></label></br></br>";																	
		}
		else {
			echo "<span class='pageText'>".parent::getAdminText("manageUserRightsForm", "roleAdmin")."</span>	<Input type ='radio' id='radio-1-1' name ='rights' value= 'admin' class='radio' ><label for='radio-1-1'></label>
			&nbsp;<span class='black'>|</span>&nbsp;";
			echo "<span class='pageText'>".parent::getAdminText("manageUserRightsForm", "roleUser")."</span>	<Input type ='radio' id='radio-1-2' name ='rights' value= 'user' class='radio' checked><label for='radio-1-2'></label></br></br>";
		}
		echo "<input class='submit' type='submit' name ='changeRights' value='".parent::getAdminText("manageUserRightsForm", "submitButton")."'>";
		echo "<input class='submit' type='reset' name='resetButton' value='".parent::getAdminText("manageUserRightsForm", "resetButton")."'>";

		echo "</form>";
	
	}

}

}
?>